package cn.Alibaba.basic.service.impl;

import cn.Alibaba.basic.mapper.BaseMapper;
import cn.Alibaba.basic.query.BaseQuery;
import cn.Alibaba.basic.service.IBaseService;
import cn.Alibaba.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;


    @Override
    public List<T> findAll() {
        return  baseMapper.loadAll();
    }

    @Override
    public T findOne(Long id) {
        return  baseMapper.loadOne(id);
    }

    @Transactional
    @Override
    public void save(T t) {

        baseMapper.add(t);

    }

    @Transactional
    @Override
    public void update(T t) {
        baseMapper.update(t);

    }

    @Transactional
    @Override
    public void del(Long id) {
        baseMapper.delete(id);

    }

    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        Integer count = baseMapper.queryCount(baseQuery);
        if (count==null || count==0){
            return new PageList<T>();
        }
        List<T> list = (List<T>) baseMapper.queryData(baseQuery);
        return new PageList<T>(count,list);
    }

    @Override
    public void PatchDel(List ids) {
        baseMapper.PatchDel( ids);
    }
}
